#include <learn/check_gl.h>
#include <learn/geom/vec.h>

constexpr float TWO_PI = 6.2831853f;

GLuint regHex;

void init()
{
    glEnable(GL_LINE_STIPPLE);
    glLineStipple(1, 0xff);

    // 注册显示表
    regHex = glGenLists(1);
    glNewList(regHex, GL_COMPILE);

    // 显示表内容
    glBegin(GL_LINE_LOOP);
    for (int i = 0; i < 6; i++)
    {
        double theta = TWO_PI * i / 6.0;
        double x = 0.8 * cos(theta);
        double y = 0.8 * sin(theta);
        glColor3f((x + 1) / 2, (y + 1) / 2, 0);
        glVertex3f(x, y, 0);
    }
    glEnd();

    // 结束命令
    glEndList();
}

void render()
{
    // 调用显示表
    glCallList(regHex);
}

int main()
{
    if (!glfwInit())
    {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    GLFWwindow *window = glfwCreateWindow(800, 600, "Three Stars", nullptr, nullptr);
    if (!window)
    {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    init();
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}
